package com.taoyibao.hongcanting.dialog;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author TangAnna
 * @description: 版本更新
 * @date :${DATA} 10:19
 */
@SuppressLint("ValidFragment")
public class DialogUpdataVersion extends BaseFragmentDialog implements View.OnClickListener, DialogInterface.OnKeyListener {
    private ImageView mIvFinish;
    private TextView mTvVersionName;
    private RecyclerView mRecyclerView;
    private List<String> mVersioContextData;//版本更新的内容集合
    /**
     * 是否是强制更新  1强制更新0非强制更新；
     */
    private int isForce;

    /**
     * 版本名称
     */
    private String mVersionName;
    private String mVersionContext;//更新内容

    public void setUIData(int isForce, String versionName, String versionContext) {
        this.isForce = isForce;
        mVersionName = versionName;
        mVersionContext = versionContext;
    }

    @Override
    protected int bindView() {
        return R.layout.dialog_updata_version;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        //关闭按钮
        if (isForce == CodeConstant.APP_UPDATA_FORCE) {
            view.findViewById(R.id.iv_dialogVersion_finish).setVisibility(View.GONE);
        } else {
            view.findViewById(R.id.iv_dialogVersion_finish).setVisibility(View.VISIBLE);
        }
        //版本名称
        mTvVersionName = view.findViewById(R.id.tv_dialogVersion_versionName);
        mRecyclerView = view.findViewById(R.id.rv_dialogVersion_versionContext);
    }

    @Override
    protected void setData() {
        super.setData();
        //设置版本名称
        if (!TextUtils.isEmpty(mVersionName)) {
            mTvVersionName.setText(mVersionName);
        }
        setOnkeyListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setAdapter(new CommonAdapter<String>(getContext(), R.layout.item_updata_version, mVersioContextData) {
            @Override
            protected void convert(ViewHolder holder, String s, int position) {
                TextView textView = holder.getView(R.id.tv_itemUpDataVersion_text);
                textView.setText(s.trim());
            }
        });
    }


    @Override
    protected void initData() {
        super.initData();
        mVersioContextData = new ArrayList<>();
        if (!TextUtils.isEmpty(mVersionContext.trim())) {
            if (mVersionContext.trim().contains(CodeConstant.ZENG_SONG)) {
                String[] split = mVersionContext.trim().split(CodeConstant.ZENG_SONG);
                mVersioContextData.addAll(Arrays.asList(split));
                mVersioContextData.remove("");
            } else {
                mVersioContextData.add(mVersionContext);
            }

        }
    }


    @Override
    protected void setListener(View view) {
        super.setListener(view);
        view.findViewById(R.id.btn_dialogVersion_updata).setOnClickListener(this);
        view.findViewById(R.id.iv_dialogVersion_finish).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_dialogVersion_updata://立即更新
                if (mDownloadInter != null) {
                    mDownloadInter.onDownload(isForce);
                }
                break;
            case R.id.iv_dialogVersion_finish://关闭按钮
                dismiss();
                break;
        }
    }

    @Override
    protected boolean setCanceledOnTouchOutside() {
        return false;
    }

    @Override
    protected boolean setCanCancel() {
        return false;
    }

    @Override
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return false;
    }

    private DialogDownloadInter mDownloadInter;

    /**
     * 设置下载的事件
     *
     * @param downloadInter
     */
    public void setDownloadInter(DialogDownloadInter downloadInter) {
        mDownloadInter = downloadInter;
    }

    public interface DialogDownloadInter {
        void onDownload(int isForce);
    }
}
